import numpy as np
import scipy
from scipy.signal import butter, filtfilt
from scipy.io import wavfile
from mne.io import read_raw_edf
import os
import librosa
from pynwb import NWBHDF5IO

import pandas as pd
from pydub import AudioSegment 


hilbert3 = lambda x: scipy.signal.hilbert(x, scipy.fftpack.next_fast_len(len(x)),axis=0)[:len(x)]
def z_score_normalization(data):
    mean = np.mean(data, axis=0, keepdims=True)
    std = np.std(data, axis=0, keepdims=True)
    return (data - mean) / std


if __name__ == '__main__':
    eeg_path = './cut_data_zscore'      # original file path
    outputs = './npy_data_class_2_zscore'    # output feature

    time_files = os.listdir(eeg_path)
    for data_id in ['train', 'val']:
        file_names = os.listdir(os.path.join(eeg_path, data_id))
        for file in file_names:
            # neural data
            eeg_data = np.load(os.path.join(eeg_path, data_id, file))  # read data
            eeg_data = eeg_data.transpose((1, 0))
            # extract HG feature     window average
            eeg_data = eeg_data[:6000, :]
            # eeg_data = scipy.signal.detrend(eeg_data, axis=0)
            # eeg_data = np.abs(hilbert3(eeg_data))
            # eeg_data = z_score_normalization(eeg_data)
            np.save(os.path.join(f'./{outputs}/{data_id}/{file}'), eeg_data)

